<?php

namespace Api\Member\AccessLog\Open;

use Api\RepositoryResourceTrait;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;

/**
 * 资源仓库
 */
class AccessLogRepository
{
    use RepositoryResourceTrait;

    /**
     * 允许列表查询的每页最大条目
     *
     * @var int
     */
    private $allowMaxPerPage = 100;

    /**
     * 可见字段
     *
     * @var array
     */
    private $visible = [
        'id',
        'date',
        'total',
        'updated_at',
        'created_at',
    ];

    /**
     * 构造方法
     */
    public function __construct(
        \Api\Member\AccessLog\Models\AccessLog $Model
    ) {
        $this->Models = compact('Model');
    }

    /**
     * 累计访问次数
     *
     * @param string $walletAddress
     * @param int $sendCoin
     * @return void
     */
    public function log()
    {
        // 加锁操作
        DB::beginTransaction();

        $log = $this->Model->where('date', Carbon::today())->first() ?: $this->Model;
        $log->date   = Carbon::today();
        $log->total += 1;
        $log->saveOrFail();
        
        // 事务提交
        DB::commit();
    }


}
